import tkinter as tk
import tkinter.messagebox as tkm
import numpy as np
import tkinter.ttk as ttk

intro_text = '欢迎参与数独游戏，你可以按照提示进行游戏\n'
intro_text_1 = '1.完成当前游戏后，可以点击‘Submit’按钮进行检查。\n2.点击‘clear’按钮可以清空当前已填内容，重新填写。\n3.点击‘Restart’按钮后，可以开始一局新的数独游戏。\n'
sudok_puzzle = np.loadtxt('Sudok_puzzle.txt', dtype=bytes).astype(int)
sudok_puzzle = sudok_puzzle.reshape(-1, 81)
sudok_puzzle_index = 0


def restart(passhold):
    global sudok_puzzle_index
    sudok_puzzle_index = sudok_puzzle_index + 1
    print(sudok_puzzle_index)
    show = GridManager()
    show.make(sudok_puzzle[sudok_puzzle_index])


class GridManager():
    def check_sudok_map(sudok_map):  # 生成终局的正确性检查
        """
        After the gen_all_map Function.
        This function is to check the map whether correct or not.
        Based on the Zone_Index algorithm.
        :param
            sudok_map_check:the sudok_map which gen_all_map generated.
        :return:
        """
        sudok_map_check = np.array(sudok_map)
        sudok_map_check = sudok_map_check.reshape(9, 9)
        print(sudok_map_check)
        temp_line = [1, 2, 3, 4, 5, 6, 7, 8, 9]
        temp_map = []
        for i in range(9):
            temp_map.append(temp_line.copy())
        for row in range(9):
            for column in range(9):
                blockindex = (row // 3) * 3 + column // 3
                temp_map[blockindex][sudok_map_check[row][column] - 1] = -1
        for item_check in temp_map:
            temp_sum = 0
            for i in range(9):
                temp_sum += item_check[i]
            if temp_sum != -9:
                return False
        return True

    def submit(sudok_puzzle, entrylist, entryindex):
        print(entryindex)
        check_sudok = sudok_puzzle.copy()
        sub_index = 0
        for item in entrylist:
            if item.get() == '':
                mes = tkm.showinfo('提示', '请先完成数独游戏！')
                mes.destory()
            elif item.get() not in ['1', '2', '3', '4', '5', '6', '7', '8', '9']:
                mes = tkm.showinfo('提示', '存在非法输入，请检查！')
                mes.destory()
            else:
                check_sudok[entryindex[sub_index]] = int(item.get())
            sub_index += 1
        if GridManager.check_sudok_map(check_sudok) == True:
            mes = tkm.showinfo('信息', '太棒了')
            mes.destory()
        else:
            mes = tkm.showinfo('信息', '请再试一次')
            mes.destory()
        print(check_sudok)

    def clear(entrylist):
        for item in entrylist:
            item.delete(0, 10)

    def make(self, sudok_map):
        colist = []
        colindex = []
        sudok_map_copy = sudok_map.copy()
        window = tk.Tk()
        window.geometry('750x780+400+10')
        window.title("数独游戏")  # Set title
        # 导航栏
        menubar = tk.Menu(window)
        filemenu = tk.Menu(menubar, tearoff=0)
        menubar.add_cascade(label='开始', menu=filemenu)
        menubar.add_cascade(label='继续', menu=filemenu)
        menubar.add_cascade(label='记录', menu=filemenu)
        menubar.add_cascade(label='帮助', menu=filemenu)
        filemenu.add_command(label='说明')
        window.config(menu=menubar)

        frame_0 = tk.Frame(window, bd=1, borderwidth=1, highlightthickness=1, highlightbackground='black')
        frame_0.pack()
        Intro_1 = tk.Label(frame_0, text=intro_text, font=("微软雅黑", 12), padx=0, anchor=tk.W)
        Intro_1.pack()
        Intro_2 = tk.Label(frame_0, text=intro_text_1, font=("微软雅黑", 10), justify=tk.LEFT)
        Intro_2.pack()

        ##设计框架存放组
        frame = tk.Frame(window)
        frame.pack()

        frame_1 = tk.Frame(frame, highlightthickness=1, highlightcolor='Gold')
        frame_1.pack(side='left', fill='x')

        frame_3 = tk.Frame(frame, highlightthickness=1, highlightcolor='Gold')
        frame_3.pack(side='right', fill='x')

        frame_2 = tk.Frame(frame, highlightthickness=1, highlightcolor='Gold')
        frame_2.pack(expand='yes')

        for i in range(3):
            for j in range(3):
                if (sudok_map_copy[i * 9 + j] == 0):
                    # col_1 = tk.Entry(frame_1, bg='lightpink', width=1, textvariable=self.num)
                    col_1 = tk.Entry(frame_1, bg='lightpink', width=2, relief='flat')
                    col_1.grid(row=i, column=j, padx=10, pady=10, ipadx=10, ipady=10)
                    colist.append(col_1)
                    colindex.append(i * 9 + j)
                    # print(col_1.get())
                    # if col_1.get() != '':
                    #     sudok_map_copy[i * 9 + j] = int(col_1.get())
                    #     print(col_1.get())
                    # else:
                    #     sudok_map_copy[i * 9 + j] = 0
                else:
                    tk.Label(frame_1, text=sudok_map_copy[i * 9 + j], bg='lightpink', width=2).grid(row=i, column=j,
                                                                                                    padx=10,
                                                                                                    pady=10,
                                                                                                    ipadx=10, ipady=10)

        for i in range(3):
            for j in range(3):
                if (sudok_map_copy[i * 9 + j + 3] == 0):
                    col_2 = tk.Entry(frame_2, bg='lightblue', width=2, relief='flat')
                    col_2.grid(row=i, column=j, padx=10, pady=10, ipadx=10, ipady=10)
                    colist.append(col_2)
                    colindex.append(i * 9 + j + 3)
                else:
                    tk.Label(frame_2, text=sudok_map_copy[i * 9 + j + 3], bg='lightblue', width=2).grid(row=i, column=j,
                                                                                                        padx=10,
                                                                                                        pady=10,
                                                                                                        ipadx=10,
                                                                                                        ipady=10)

        for i in range(3):
            for j in range(3):
                if (sudok_map_copy[i * 9 + j + 6] == 0):
                    col_3 = tk.Entry(frame_3, bg='lightyellow', width=2, relief='flat')
                    col_3.grid(row=i, column=j, padx=10, pady=10, ipadx=10, ipady=10)
                    colist.append(col_3)
                    colindex.append(i * 9 + j + 6)
                else:
                    tk.Label(frame_3, text=sudok_map_copy[i * 9 + j + 6], bg='lightyellow', width=2).grid(row=i,
                                                                                                          column=j,
                                                                                                          padx=10,
                                                                                                          pady=10,
                                                                                                          ipadx=10,
                                                                                                          ipady=10)

        ##设计框架存放组
        frame1 = tk.Frame(window)
        frame1.pack()

        frame_5 = tk.Frame(frame1, highlightthickness=1, highlightcolor='Gold')
        frame_5.pack(side='left', fill='x')

        frame_6 = tk.Frame(frame1, highlightthickness=1, highlightcolor='Gold')
        frame_6.pack(side='right', fill='x')

        frame_4 = tk.Frame(frame1, highlightthickness=1, highlightcolor='Gold')
        frame_4.pack(expand='yes')

        for i in range(3):
            for j in range(3):
                if (sudok_map_copy[i * 9 + j + 27] == 0):
                    col_4 = tk.Entry(frame_5, bg='lightyellow', width=2, relief='flat')
                    col_4.grid(row=i, column=j, padx=10, pady=10, ipadx=10, ipady=10)
                    colist.append(col_4)
                    colindex.append(i * 9 + j + 27)
                else:
                    tk.Label(frame_5, text=sudok_map_copy[i * 9 + j + 27], bg='lightyellow', width=2).grid(row=i,
                                                                                                           column=j,
                                                                                                           padx=10,
                                                                                                           pady=10,
                                                                                                           ipadx=10,
                                                                                                           ipady=10)

        for i in range(3):
            for j in range(3):
                if (sudok_map_copy[i * 9 + j + 30] == 0):
                    col_5 = tk.Entry(frame_4, bg='lightpink', width=2, relief='flat')
                    col_5.grid(row=i, column=j, padx=10, pady=10, ipadx=10, ipady=10)
                    colist.append(col_5)
                    colindex.append(i * 9 + j + 30)
                else:
                    tk.Label(frame_4, text=sudok_map_copy[i * 9 + j + 30], bg='lightpink', width=2).grid(row=i,
                                                                                                         column=j,
                                                                                                         padx=10,
                                                                                                         pady=10,
                                                                                                         ipadx=10,
                                                                                                         ipady=10)
        for i in range(3):
            for j in range(3):
                if (sudok_map_copy[i * 9 + j + 33] == 0):
                    col_6 = tk.Entry(frame_6, bg='lightblue', width=2, relief='flat')
                    col_6.grid(row=i, column=j, padx=10, pady=10, ipadx=10, ipady=10)
                    colist.append(col_6)
                    colindex.append(i * 9 + j + 33)
                else:
                    tk.Label(frame_6, text=sudok_map_copy[i * 9 + j + 33], bg='lightblue', width=2).grid(row=i,
                                                                                                         column=j,
                                                                                                         padx=10,
                                                                                                         pady=10,
                                                                                                         ipadx=10,
                                                                                                         ipady=10)

        ##设计框架存放组
        frame2 = tk.Frame(window)
        frame2.pack()

        frame_9 = tk.Frame(frame2, highlightthickness=1, highlightcolor='Gold')
        frame_9.pack(side='left', fill='y')

        frame_7 = tk.Frame(frame2, highlightthickness=1, highlightcolor='Gold')
        frame_7.pack(side='right', fill='x')

        frame_8 = tk.Frame(frame2, highlightthickness=1, highlightcolor='Gold')
        frame_8.pack(expand='yes')

        for i in range(3):
            for j in range(3):
                if (sudok_map_copy[i * 9 + j + 54] == 0):
                    col_7 = tk.Entry(frame_9, bg='lightblue', width=2, relief='flat')
                    col_7.grid(row=i, column=j, padx=10, pady=10, ipadx=10, ipady=10)
                    colist.append(col_7)
                    colindex.append(i * 9 + j + 54)
                else:
                    tk.Label(frame_9, text=sudok_map_copy[i * 9 + j + 54], bg='lightblue', width=2).grid(row=i,
                                                                                                         column=j,
                                                                                                         padx=10,
                                                                                                         pady=10,
                                                                                                         ipadx=10,
                                                                                                         ipady=10)
        for i in range(3):
            for j in range(3):
                if (sudok_map_copy[i * 9 + j + 57] == 0):
                    col_8 = tk.Entry(frame_8, bg='lightyellow', width=2, relief='flat')
                    col_8.grid(row=i, column=j, padx=10, pady=10, ipadx=10, ipady=10)
                    colist.append(col_8)
                    colindex.append(i * 9 + j + 57)
                else:
                    tk.Label(frame_8, text=sudok_map_copy[i * 9 + j + 57], bg='lightyellow', width=2).grid(row=i,
                                                                                                           column=j,
                                                                                                           padx=10,
                                                                                                           pady=10,
                                                                                                           ipadx=10,
                                                                                                           ipady=10)

        for i in range(3):
            for j in range(3):
                if (sudok_map_copy[i * 9 + j + 60] == 0):
                    col_9 = tk.Entry(frame_7, bg='lightpink', width=2, relief='flat')
                    col_9.grid(row=i, column=j, padx=10, pady=10, ipadx=10, ipady=10)
                    colist.append(col_9)
                    colindex.append(i * 9 + j + 60)
                else:
                    tk.Label(frame_7, text=sudok_map_copy[i * 9 + j + 60], bg='lightpink', width=2).grid(row=i,
                                                                                                         column=j,
                                                                                                         padx=10,
                                                                                                         pady=10,
                                                                                                         ipadx=10,
                                                                                                         ipady=10)
        frame3 = tk.Frame(window)
        frame3.pack()

        # 设计提交检测按钮
        sub_button_1 = tk.Button(frame3, relief='flat', text='Submit', bd=3, bg='LightCyan',
                                 command=lambda: GridManager.submit(sudok_map_copy, colist, colindex))
        sub_button_1.grid(row=1, column=0, padx=10, pady=10, ipadx=10, ipady=10)
        # sub_button_1.pack()

        # 设计清空已填内容按钮
        sub_button_2 = tk.Button(frame3, relief='flat', text='clear', bd=3, bg='LightCyan',
                                 command=lambda: GridManager.clear(colist))
        sub_button_2.grid(row=1, column=1, padx=10, pady=10, ipadx=10, ipady=10)
        # sub_button_2.pack()

        # 设计重新开始按钮
        sub_button_3 = tk.Button(frame3, relief='flat', text='Restart', bd=3, bg='LightCyan',
                                 command=lambda: restart(window.destroy()))
        sub_button_3.grid(row=1, column=2, padx=10, pady=10, ipadx=10, ipady=10)

        window.mainloop()


if __name__ == "__main__":
    # print(sudok_puzzle)
    show = GridManager()
    show.make(sudok_puzzle[0])
